home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / lib / post_parser.php < prev    next >
Encoding:
PHP Script  |  2002-06-12  |  32.1 KB  |  972 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > Text processor module
  17. |   > Module written by Matt Mecham
  18. |
  19. +--------------------------------------------------------------------------
  20. */
  21.  
  22.  
  23. class post_parser {
  24.  
  25.     var $error          = "";
  26.     var $image_count    = 0;
  27.     var $emoticon_count = 0;
  28.     var $quote_html     = array();
  29.     var $quote_open     = 0;
  30.     var $quote_closed   = 0;
  31.     var $quote_error    = 0;
  32.     var $emoticons      = "";
  33.     var $badwords       = "";
  34.     
  35.     function smilie_length_sort($a, $b)
  36.     {
  37.         if ( strlen($a['typed']) == strlen($b['typed']) )
  38.         {
  39.             return 0;
  40.         }
  41.         return ( strlen($a['typed']) > strlen($b['typed']) ) ? -1 : 1;
  42.     }
  43.     
  44.     
  45.     function word_length_sort($a, $b)
  46.     {
  47.         if ( strlen($a['type']) == strlen($b['type']) )
  48.         {
  49.             return 0;
  50.         }
  51.         return ( strlen($a['type']) > strlen($b['type']) ) ? -1 : 1;
  52.     }
  53.     
  54.     
  55.     function post_parser($load=0)
  56.     {
  57.         global $ibforums, $DB;
  58.         
  59.         if ($load != 0)
  60.         {
  61.             // Pre-load the bad words
  62.             
  63.             $DB->query("SELECT * from ibf_badwords");
  64.             
  65.             if ( $DB->get_num_rows() )
  66.             {
  67.                 while ( $r = $DB->fetch_row() )
  68.                 {
  69.                     $this->badwords[] = array( 'type'    => stripslashes($r['type']),
  70.                                                'swop'    => stripslashes($r['swop']),
  71.                                                'm_exact' => $r['m_exact'],
  72.                                              );
  73.                 }
  74.             }
  75.             
  76.             // Pre-load the smilies
  77.             
  78.             $this->emoticons = array();
  79.         
  80.             $DB->query("SELECT typed, image from ibf_emoticons");
  81.             
  82.             if ( $DB->get_num_rows() )
  83.             {
  84.                 while ( $r = $DB->fetch_row() )
  85.                 {
  86.                     $this->emoticons[] = array( 'typed'     => stripslashes($r['typed']),
  87.                                                 'image'     => stripslashes($r['image']),
  88.                                                 'clickable' => $r['clickable'],
  89.                                               );
  90.                 }
  91.             }
  92.             
  93.         }
  94.         
  95.     }
  96.     
  97.     /**************************************************/
  98.     // convert:
  99.     // Parses raw text into smilies, HTML and iB CODE
  100.     /**************************************************/
  101.  
  102.     function convert($in=array( 'TEXT' => "", 'SMILIES' => 0, 'CODE' => 0, 'SIGNATURE' => 0, 'HTML' => 0)) {
  103.         global $ibforums, $DB;
  104.         
  105.         $txt = $in['TEXT'];
  106.     
  107.         //--------------------------------------
  108.         // Returns any errors as $this->error
  109.         //--------------------------------------
  110.         
  111.         // Remove session id's from any post
  112.         
  113.         $txt = preg_replace( "#[?&;]s=([0-9][a-z][A-Z]){32}#", "", $txt );
  114.         
  115.         //--------------------------------------
  116.         // convert <br> to \n
  117.         //--------------------------------------
  118.         
  119.         $txt = preg_replace( "/<br>|<br\s*\/>/", "\n", $txt );
  120.         
  121.         //--------------------------------------
  122.         // Are we parsing iB_CODE and do we have either '[' or ']' in the
  123.         // text we are processing?
  124.         //--------------------------------------
  125.         
  126.         if ( $in['CODE'] == 1 ) {
  127.         
  128.             //--------------------------------------
  129.             // Auto parse URLs
  130.             //--------------------------------------
  131.             
  132.             $txt = preg_replace( "#(^|\s)((http|https|news|ftp)://\w+\S+)#ie"  , "\$this->regex_build_url(array('html' => '\\2', 'show' => '\\2', 'st' => '\\1'))", $txt );
  133.             
  134.         
  135.             //---------------------------------
  136.             // Do [CODE] tag
  137.             //---------------------------------
  138.             
  139.             $txt = preg_replace( "#\[code\](.+?)\[/code\]#ies", "\$this->regex_code_tag('\\1')", $txt );
  140.         
  141.             //---------------------------------
  142.             // Do [QUOTE(name,date)] tags
  143.             //---------------------------------
  144.             
  145.             // Find the first, and last quote tag (greedy match)...
  146.             
  147.             $txt = preg_replace( "#(\[quote.*\].*\[/quote\])#ies" , "\$this->regex_parse_quotes('\\1')"  , $txt );
  148.             
  149.             /***********************************************/
  150.             // If we are not parsing a siggie, lets have a bash
  151.             // at the [PHP] [SQL] and [HTML] tags.
  152.             /***********************************************/
  153.             
  154.             if ($in['SIGNATURE'] != 1) {
  155.                 
  156.                 $txt = preg_replace( "#\[sql\](.+?)\[/sql\]#ies"    , "\$this->regex_sql_tag('\\1')"    , $txt );
  157.                 $txt = preg_replace( "#\[html\](.+?)\[/html\]#ies"  , "\$this->regex_html_tag('\\1')"   , $txt );
  158.                 
  159.                 // [LIST]    [*]    [/LIST]
  160.                 //-------------------------
  161.                 $txt = preg_replace( "#\[list\]#i"  , "<ul>" , $txt );
  162.                 $txt = preg_replace( "#\[\*\]#"     , "<li>" , $txt );
  163.                 $txt = preg_replace( "#\[/list\]#i" , "</ul>", $txt );
  164.                 
  165.             }
  166.              
  167.             //---------------------------------
  168.             // Do [IMG] [FLASH] tags
  169.             //---------------------------------
  170.             
  171.             if ($ibforums->vars['allow_images'])
  172.             {
  173.                 $txt = preg_replace( "#\[img\](.+?)\[/img\]#ie"                             , "\$this->regex_check_image('\\1')"          , $txt );
  174.                 $txt = preg_replace( "#(\[flash=)(\S+?)(\,)(\S+?)(\])(\S+?)(\[\/flash\])#ie", "\$this->regex_check_flash('\\2','\\4','\\6')", $txt );
  175.             }
  176.         
  177.         
  178.             // Start off with the easy stuff
  179.             
  180.             $txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "<b>\\1</b>", $txt );
  181.             $txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>\\1</i>", $txt );
  182.             $txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>\\1</u>", $txt );
  183.             $txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "<s>\\1</s>", $txt );
  184.             
  185.             // (c) (r) and (tm)
  186.             
  187.             $txt = preg_replace( "#\(c\)#i"     , "©" , $txt );
  188.             $txt = preg_replace( "#\(tm\)#i"    , "™" , $txt );
  189.             $txt = preg_replace( "#\(r\)#i"     , "®"  , $txt );
  190.             
  191.             // font size, colour and font style
  192.             // [font=courier]Text here[/font]  [size=6]Text here[/size]  [color=red]Text here[/color]
  193.             
  194.             while ( preg_match( "#\[size=([^\]]+)\](.+?)\[/size\]#ies", $txt ) )
  195.             {
  196.                 $txt = preg_replace( "#\[size=([^\]]+)\](.+?)\[/size\]#ies"    , "\$this->regex_font_attr(array('s'=>'size','1'=>'\\1','2'=>'\\2'))", $txt );
  197.             }
  198.             
  199.             while ( preg_match( "#\[font=([^\]]+)\](.*?)\[/font\]#ies", $txt ) )
  200.             {
  201.                 $txt = preg_replace( "#\[font=([^\]]+)\](.*?)\[/font\]#ies"    , "\$this->regex_font_attr(array('s'=>'font','1'=>'\\1','2'=>'\\2'))", $txt );
  202.             }
  203.             
  204.             while( preg_match( "#\[color=([^\]]+)\](.+?)\[/color\]#ies", $txt ) )
  205.             {
  206.                 $txt = preg_replace( "#\[color=([^\]]+)\](.+?)\[/color\]#ies"  , "\$this->regex_font_attr(array('s'=>'col' ,'1'=>'\\1','2'=>'\\2'))", $txt );
  207.             }
  208.             
  209.             // email tags
  210.             // [email]matt@index.com[/email]   [email=matt@index.com]Email me[/email]
  211.             
  212.             $txt = preg_replace( "#\[email\](\S+?)\[/email\]#i"                                                                , "<a href='mailto:\\1'>\\1</a>", $txt );
  213.             $txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i"  , "<a href='mailto:\\1'>\\2</a>", $txt );
  214.             $txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i"                       , "<a href='mailto:\\1'>\\2</a>", $txt );
  215.             
  216.             // url tags
  217.             // [url]http://www.index.com[/url]   [url=http://www.index.com]ibforums![/url]
  218.             
  219.             $txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie"                                       , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
  220.             $txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#ie" , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
  221.             $txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie"                       , "\$this->regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
  222.             
  223.         }
  224.         
  225.         // Swop \n back to <br>
  226.         
  227.         $txt = preg_replace( "/\n/", "<br>", $txt );
  228.         
  229.         //+---------------------------------------------------------------------------------------------------
  230.         // Parse smilies (disallow smilies in siggies, or we'll have to query the DB for each post
  231.         // and each signature when viewing a topic, not something that we really want to do.
  232.         //+---------------------------------------------------------------------------------------------------
  233.         
  234.         if ($in['SMILIES'] != 0 and $in['SIGNATURE'] == 0) {
  235.         
  236.             if ( ! is_array($this->emoticons) )
  237.             {
  238.                 $DB->query("SELECT typed, image from ibf_emoticons");
  239.                 
  240.                 $this->emoticons = array();
  241.                 
  242.                 if ( $DB->get_num_rows() )
  243.                 {
  244.                     while ( $r = $DB->fetch_row() )
  245.                     {
  246.                         $this->emoticons[] = array( 'typed'     => stripslashes($r['typed']),
  247.                                                     'image'     => stripslashes($r['image']),
  248.                                                     'clickable' => $r['clickable'],
  249.                                                   );
  250.                     }
  251.                 }
  252.             }
  253.             
  254.             usort($this->emoticons, array( 'post_parser', 'smilie_length_sort' ) );
  255.             
  256.             if ( count($this->emoticons) > 0 )
  257.             {
  258.             
  259.                 foreach($this->emoticons as $a_id => $row)
  260.                 {
  261.                     
  262.                     $code  = $row['typed'];
  263.                     $image = $row['image'];
  264.                     
  265.                     /*$code = str_replace( "&", "&", $code );
  266.                     $code = str_replace( "<", "<" , $code );
  267.                     $code = str_replace( ">", ">" , $code );
  268.                     $code = str_replace( "\"", """, $code);
  269.                     $code = str_replace( "'", "'"  , $code);
  270.                     $code = str_replace( "!", "!"  , $code);
  271.                     $code = str_replace( "|", "|"  , $code);*/
  272.                     
  273.                     // Make safe for regex
  274.                     
  275.                     $code = preg_quote($code, "/");
  276.                     
  277.                     $txt = preg_replace( "!(?<=[^\w&])$code(?=.\W|\W.|\W$)!ei", "\$this->convert_emoticon('$code', '$image')", ' '.$txt.' ' );
  278.                     
  279.                 }
  280.             
  281.             }
  282.             
  283.             if ($ibforums->vars['max_emos'])
  284.             {
  285.                 if ($this->emoticon_count > $ibforums->vars['max_emos'])
  286.                 {
  287.                     $this->error = 'too_many_emoticons';
  288.                 }
  289.             }
  290.         
  291.         }
  292.         
  293.         $txt = $this->bad_words($txt);
  294.         
  295.         if ($in['HTML'] == 1)
  296.         {
  297.             $txt = str_replace( "<"    ,"<" , $txt );
  298.             $txt = str_replace( ">"    ,">" , $txt );
  299.             $txt = str_replace( """  ,"\"", $txt);
  300.             $txt = str_replace( "'"  ,"'" , $txt);
  301.             $txt = str_replace( "&"   ,"&" , $txt );
  302.         }
  303.         
  304.         return $txt; //trim($txt);
  305.     }
  306.     
  307.     /**************************************************/
  308.     // Badwords:
  309.     // Swops naughty, naugty words and stuff
  310.     /**************************************************/
  311.     
  312.     function bad_words($text = "")
  313.     {
  314.         global $DB, $ibforums;
  315.         
  316.         if ($text == "")
  317.         {
  318.             return "";
  319.         }
  320.         
  321.         //--------------------------------
  322.         
  323.         if ( ! is_array($this->badwords) )
  324.         {
  325.             $DB->query("SELECT * from ibf_badwords");
  326.             
  327.             $this->badwords = array();
  328.             
  329.             if ( $DB->get_num_rows() )
  330.             {
  331.                 while ( $r = $DB->fetch_row() )
  332.                 {
  333.                     $this->badwords[] = array( 'type'    => stripslashes($r['type']),
  334.                                                'swop'    => stripslashes($r['swop']),
  335.                                                'm_exact' => $r['m_exact'],
  336.                                              );
  337.                 }
  338.             }
  339.         }
  340.         
  341.         usort($this->badwords, array( 'post_parser', 'word_length_sort' ) );
  342.         
  343.         if ( count($this->badwords) > 0 )
  344.         {
  345.             
  346.             foreach($this->badwords as $idx => $r)
  347.             {
  348.             
  349.                 if ($r['swop'] == "")
  350.                 {
  351.                     $replace = '######';
  352.                 }
  353.                 else
  354.                 {
  355.                     $replace = $r['swop'];
  356.                 }
  357.                 
  358.                 //---------------------------
  359.                 
  360.                 $r['type'] = preg_quote($r['type'], "/");
  361.                 
  362.                 //---------------------------
  363.             
  364.                 if ($r['m_exact'] == 1)
  365.                 {
  366.                     $text = preg_replace( "/(^|\b)".$r['type']."(\b|!|\?|\.|,|$)/i", "$replace", $text );
  367.                 }
  368.                 else
  369.                 {
  370.                     $text = preg_replace( "/".$r['type']."/i", "$replace", $text );
  371.                 }
  372.             }
  373.         
  374.         
  375.         }
  376.         
  377.         return $text;
  378.         
  379.     }
  380.     
  381.     
  382.     /**************************************************/
  383.     // unconvert:
  384.     // Parses the HTML back into plain text
  385.     /**************************************************/
  386.         
  387.     function unconvert($txt="", $code=1, $html=0) {
  388.     
  389.         if ($code == 1)
  390.         {
  391.             $txt = preg_replace( "#<!--emo&(.+?)-->.+?<!--endemo-->#", "\\1" , $txt );
  392.             
  393.             $txt = preg_replace( "#<!--sql-->(.+?)<!--sql1-->(.+?)<!--sql2-->(.+?)<!--sql3-->#e"    , "\$this->unconvert_sql(\"\\2\")", $txt);
  394.             $txt = preg_replace( "#<!--html-->(.+?)<!--html1-->(.+?)<!--html2-->(.+?)<!--html3-->#e", "\$this->unconvert_htm(\"\\2\")", $txt);
  395.         
  396.             $txt = preg_replace( "#<!--Flash (.+?)-->.+?<!--End Flash-->#e"  , "\$this->unconvert_flash('\\1')", $txt );
  397.             $txt = preg_replace( "#<img src=[\"'](\S+?)['\"].+?".">#"           , "\[IMG\]\\1\[/IMG\]"            , $txt );
  398.             $txt = preg_replace( "#<a href=[\"'](http://|https://|ftp://|news://)?(\S+?)['\"].+?".">(.+?)</a>#" , "\[URL=\\1\\2\]\\3\[/URL\]"  , $txt );
  399.             
  400.             $txt = preg_replace( "#<a href=[\"']mailto:(.+?)['\"]>(.+?)</a>#"                         , "\[EMAIL=\\1\]\\2\[/EMAIL\]"   , $txt );
  401.             
  402.             $txt = preg_replace( "#<i>(.+?)</i>#"  , "\[i\]\\1\[/i\]"  , $txt );
  403.             $txt = preg_replace( "#<b>(.+?)</b>#"  , "\[b\]\\1\[/b\]"  , $txt );
  404.             $txt = preg_replace( "#<s>(.+?)</s>#"  , "\[s\]\\1\[/s\]"  , $txt );
  405.             $txt = preg_replace( "#<u>(.+?)</u>#"  , "\[u\]\\1\[/u\]"  , $txt );
  406.             
  407.             $txt = preg_replace( "#<ul>#" , "\[LIST\]"  , $txt );
  408.             $txt = preg_replace( "#<li>#" , "\[*\]"     , $txt );
  409.             $txt = preg_replace( "#</ul>#", "\[/LIST\]" , $txt );
  410.             
  411.             $txt = preg_replace( "#<!--me&(.+?)-->(.+?)<!--e--me-->#e" , "\$this->unconvert_me('\\1', '\\2')", $txt );
  412.             
  413.             $txt = preg_replace( "#<span style=['\"]font-size:(.+?)pt;line-height:100%['\"]>(.+?)</span>#e" , "\$this->unconvert_size('\\1', '\\2')", $txt );
  414.             $txt = preg_replace( "#<span style=['\"]color:(.+?)['\"]>(.+?)</span>#"                         , "\[color=\\1\]\\2\[/color\]", $txt );
  415.             $txt = preg_replace( "#<span style=['\"]font-family:(.+?)['\"]>(.+?)</span>#"                   , "\[font=\\1\]\\2\[/font\]"  , $txt );
  416.             
  417.             $txt = preg_replace( "#<!--c1-->(.+?)<!--ec1-->#", '[CODE]'   , $txt );
  418.             $txt = preg_replace( "#<!--c2-->(.+?)<!--ec2-->#", '[/CODE]'  , $txt );
  419.             
  420.             $txt = preg_replace( "#<!--QuoteBegin-->(.+?)<!--QuoteEBegin-->#"                , '[QUOTE]'         , $txt );
  421.             $txt = preg_replace( "#<!--QuoteBegin--(.+?)\+(.+?)-->(.+?)<!--QuoteEBegin-->#"  , "[QUOTE=\\1,\\2]" , $txt );
  422.             $txt = preg_replace( "#<!--QuoteEnd-->(.+?)<!--QuoteEEnd-->#"                    , '[/QUOTE]'        , $txt );
  423.             
  424.             // Tidy up the end quote stuff
  425.             
  426.             $txt = preg_replace( "#(\[/QUOTE\])\s*?<br>\s*#si", "\\1\n", $txt );
  427.             
  428.             $txt = preg_replace( "#<!--EDIT\|.+?\|.+?-->#" , "" , $txt );
  429.         }
  430.  
  431.         if ($html == 1)
  432.         {
  433.             $txt = str_replace( "'", "'", $txt);
  434.         }
  435.         
  436.         $txt = preg_replace( "#<br>#", "\n", $txt );
  437.         
  438.         
  439.         
  440.         return trim(stripslashes($txt));
  441.     }
  442.     
  443. //+-----------------------------------------------------------------------------------------
  444. //+-----------------------------------------------------------------------------------------
  445. // UNCONVERT FUNCTIONS
  446. //+-----------------------------------------------------------------------------------------
  447. //+-----------------------------------------------------------------------------------------
  448.  
  449.     function unconvert_size($size="", $text="") {
  450.         
  451.         $size -= 7;
  452.         
  453.         return '[SIZE='.$size.']'.$text.'[/SIZE]';
  454.         
  455.     }
  456.  
  457.     function unconvert_flash($flash="") {
  458.     
  459.         $f_arr = explode( "+", $flash );
  460.         
  461.         return '[FLASH='.$f_arr[0].','.$f_arr[1].']'.$f_arr[2].'[/FLASH]';
  462.         
  463.     }
  464.     
  465.     function unconvert_me($name="", $text="") {
  466.     
  467.         $text = preg_replace( "#<span id='ME'><center>(.+?)</center></span>#", "\\1" , $text );
  468.         $text = preg_replace( "#$name#", "", $text );
  469.         
  470.         return '[ME='.$name.']'.$text.'[/ME]';
  471.         
  472.     }
  473.     
  474.     function unconvert_sql($sql="") {
  475.         $sql = stripslashes($sql);
  476.         $sql = preg_replace( "#<span style='.+?'>(.+?)</span>#", "\\1", $sql );
  477.         $sql = preg_replace( "#\s*$#"    , "", $sql );
  478.         
  479.         return '[SQL]'.$sql.'[/SQL]';
  480.         
  481.     }
  482.  
  483.     function unconvert_htm($html="") {
  484.         $html = stripslashes($html);
  485.         $html = preg_replace( "#<span style='.+?'>(.+?)</span>#", "\\1", $html );
  486.         $html = preg_replace( "#\s*$#"    , "", $html );
  487.         
  488.         return '[HTML]'.$html.'[/HTML]';
  489.         
  490.     }
  491.     
  492.     
  493. //+-----------------------------------------------------------------------------------------
  494. //+-----------------------------------------------------------------------------------------
  495. // CONVERT FUNCTIONS
  496. //+-----------------------------------------------------------------------------------------
  497. //+-----------------------------------------------------------------------------------------
  498.  
  499.     /**************************************************/
  500.     // convert_emoticon:
  501.     // replaces the text with the emoticon image
  502.     /**************************************************/
  503.     
  504.     function convert_emoticon($code="", $image="") {
  505.         global $ibforums;
  506.         
  507.         if (!$code or !$image) return;
  508.         
  509.         // Remove slashes added by preg_quote
  510.         $code = stripslashes($code);
  511.         
  512.         $this->emoticon_count++;
  513.         
  514.         return "<!--emo&$code--><img src='{$ibforums->vars['EMOTICONS_URL']}/$image' border='0' valign='absmiddle' alt='$image'><!--endemo-->";
  515.     }
  516.     
  517.     /**************************************************/
  518.     // wrap style:
  519.     // code and quote table HTML generator
  520.     /**************************************************/
  521.     
  522.     function wrap_style( $in=array() ) {
  523.         global $ibforums;
  524.         
  525.         if (! isset($in['TYPE']) )  $in['TYPE']  = 'id';
  526.         if (! isset($in['CSS']) )   $in['CSS']   = 'postcolor';
  527.         if (! isset($in['STYLE']) ) $in['STYLE'] = 'QUOTE';
  528.         
  529.         //-----------------------------
  530.         // This returns two array elements:
  531.         //  START: Contains the HTML code for the start wrapper
  532.         //  END  : Contains the HTML code for the end wrapper
  533.         //-----------------------------
  534.         
  535.         $possible_use = array( 'CODE'  => array( 'CODE',  'CODE' ),
  536.                                'QUOTE' => array( 'QUOTE', 'QUOTE'  ),
  537.                                'SQL'   => array( 'CODE' , 'SQL'),
  538.                                'HTML'  => array( 'CODE' , 'HTML'),
  539.                                'PHP'   => array( 'CODE' , 'PHP')
  540.                              );
  541.                              
  542.         return array( 'START' => "</span><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>{$possible_use[$in[STYLE]][1]}</b> {$in[EXTRA]}</td></tr><tr><td id='{$possible_use[ $in[STYLE] ][0]}'>",
  543.                       'END'   => "</td></tr></table><span {$in[TYPE]}='{$in[CSS]}'>"
  544.                     );
  545.     }
  546.  
  547.  
  548.     /**************************************************/
  549.     // regex_html_tag: HTML syntax highlighting
  550.     // 
  551.     /**************************************************/
  552.     
  553.     function regex_html_tag($html="") {
  554.     
  555.         if ($html == "") return;
  556.         
  557.         // Ensure that spacing is preserved
  558.  
  559.         // Too many embedded code/quote/html/sql tags can crash Opera and Moz
  560.         
  561.         if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $html) ) {
  562.             return $default;
  563.         }
  564.                 
  565.         //$html = preg_replace( "#\s{2}#", "  ", $html );
  566.         
  567.         // Knock off any preceeding newlines (which have
  568.         // since been converted into <br>)
  569.         
  570.         //$html = nl2br($html);
  571.         
  572.         $html = preg_replace( "/^<br>/", "", $html );
  573.         $html = preg_replace( "/^\s+/" , "", $html );
  574.         
  575.         $html = preg_replace( "#<([^&<>]+)>#"                           , "<<span style='color:blue'>\\1</span>>"        , $html );   //Matches <tag>
  576.         $html = preg_replace( "#<([^&<>]+)=#"                              , "<<span style='color:blue'>\\1</span>="           , $html );   //Matches <tag
  577.         $html = preg_replace( "#</([^&]+)>#"                            , "</<span style='color:blue'>\\1</span>>"       , $html );   //Matches </tag>
  578.         $html = preg_replace( "!=("|')(.+?)("|')(\s|>)!" , "=\\1<span style='color:orange'>\\2</span>\\3\\4"       , $html );   //Matches ='this'
  579.         $html = preg_replace( "!<!--(.+?)-->!"                    , "<!<span style='color:red'>--\\1--</span>>", $html );
  580.         
  581.         
  582.         
  583.         $wrap = $this->wrap_style( array( 'STYLE' => 'HTML' ) );
  584.         
  585.         return "<!--html-->{$wrap['START']}<!--html1-->$html<!--html2-->{$wrap['END']}<!--html3-->";
  586.     }
  587.         
  588.     /**************************************************/
  589.     // regex_sql_tag: SQL syntax highlighting
  590.     // 
  591.     /**************************************************/
  592.     
  593.     function regex_sql_tag($sql="") {
  594.         
  595.         if ($sql == "") return;
  596.  
  597.         // Too many embedded code/quote/html/sql tags can crash Opera and Moz
  598.         
  599.         if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $sql) ) {
  600.             return $default;
  601.         }
  602.                 
  603.         // Knock off any preceeding newlines (which have
  604.         // since been converted into <br>)
  605.         
  606.         $sql = preg_replace( "/^<br>/", "", $sql );
  607.         $sql = preg_replace( "/^\s+/" , "", $sql );
  608.         
  609.         // Make certain regex work..
  610.         
  611.         if (! preg_match( "/\s+$/" , $sql) )
  612.         {
  613.             $sql = $sql.' ';
  614.         }
  615.         
  616.         $sql = preg_replace( "#(=|\+|\-|>|<|~|==|\!=|LIKE|NOT LIKE|REGEXP)#i"            , "<span style='color:orange'>\\1</span>", $sql );
  617.         $sql = preg_replace( "#(MAX|AVG|SUM|COUNT|MIN)\(#i"                                    , "<span style='color:blue'>\\1</span>("    , $sql );
  618.         $sql = preg_replace( "!("|'|')(.+?)("|'|')!i"              , "<span style='color:red'>\\1\\2\\3</span>" , $sql );
  619.         $sql = preg_replace( "#\s{1,}(AND|OR)\s{1,}#i"                                         , " <span style='color:blue'>\\1</span> "    , $sql );
  620.         $sql = preg_replace( "#(WHERE|MODIFY|CHANGE|AS|DISTINCT|IN|ASC|DESC|ORDER BY)\s{1,}#i" , "<span style='color:green'>\\1</span> "   , $sql );
  621.         $sql = preg_replace( "#LIMIT\s*(\d+)\s*,\s*(\d+)#i"                                    , "<span style='color:green'>LIMIT</span> <span style='color:orange'>\\1, \\2</span>" , $sql );
  622.         $sql = preg_replace( "#(FROM|INTO)\s{1,}(\S+?)\s{1,}#i"                                , "<span style='color:green'>\\1</span> <span style='color:orange'>\\2</span> ", $sql );
  623.         $sql = preg_replace( "#(SELECT|INSERT|UPDATE|DELETE|ALTER TABLE|DROP)#i"               , "<span style='color:blue;font-weight:bold'>\\1</span>" , $sql );
  624.         
  625.         $html = $this->wrap_style( array( 'STYLE' => 'SQL' ) );
  626.         
  627.         return "<!--sql-->{$html['START']}<!--sql1-->{$sql}<!--sql2-->{$html['END']}<!--sql3-->";
  628.      }
  629.     
  630.     /**************************************************/
  631.     // regex_code_tag: Builds this code tag HTML
  632.     // 
  633.     /**************************************************/
  634.     
  635.     function regex_code_tag($txt="") {
  636.         global $ibforums;
  637.         
  638.         $default = "\[code\]$txt\[/code\]";
  639.         
  640.         if ($txt == "") return;
  641.         
  642.         // Too many embedded code/quote/html/sql tags can crash Opera and Moz
  643.         
  644.         if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/i", $txt) ) {
  645.             return $default;
  646.         }
  647.         
  648.         // Take a stab at removing most of the common
  649.         // smilie characters.
  650.         
  651.         $txt = preg_replace( "#<#"   , "<", $txt );
  652.         $txt = preg_replace( "#>#"   , ">", $txt );
  653.         $txt = preg_replace( "#"#" , """, $txt );
  654.         $txt = preg_replace( "#\s{1};#" , ";", $txt );
  655.         $txt = preg_replace( "#:#"      , ":", $txt );
  656.         $txt = preg_replace( "#\[#"     , "[", $txt );
  657.         $txt = preg_replace( "#\]#"     , "]", $txt );
  658.         $txt = preg_replace( "#\)#"     , ")", $txt );
  659.         $txt = preg_replace( "#\(#"     , "(", $txt );
  660.         $txt = preg_replace( "#\r#"     , "<br>", $txt );
  661.         $txt = preg_replace( "#\n#"     , "<br>", $txt );
  662.         
  663.         // Ensure that spacing is preserved
  664.         
  665.         $txt = preg_replace( "#\s{2}#", "  ", $txt );
  666.         
  667.         $html = $this->wrap_style( array( 'STYLE' => 'CODE' ) );
  668.         
  669.         return "<!--c1-->{$html['START']}<!--ec1-->$txt<!--c2-->{$html['END']}<!--ec2-->";
  670.         
  671.     }
  672.     
  673.     /****************************************************************************************************/
  674.     // regex_parse_quotes: Builds this quote tag HTML
  675.     // [QUOTE] .. [/QUOTE] - allows for embedded quotes
  676.     /**************************************************/
  677.     
  678.     function regex_parse_quotes($the_txt="") {
  679.         
  680.         if ($the_txt == "") return;
  681.         
  682.         $txt = $the_txt;
  683.         
  684.         // Too many embedded code/quote/html/sql tags can crash Opera and Moz
  685.         
  686.         /*if (preg_match( "/\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\].+?\[(quote|code|html|sql)\]/is", $txt) ) {
  687.             $this->quote_error++;
  688.             return $txt;
  689.         }*/
  690.         
  691.         $this->quote_html = $this->wrap_style( array( 'STYLE' => 'QUOTE' ) );
  692.         
  693.         $txt = preg_replace( "#\[quote\]#ie"                , "\$this->regex_simple_quote_tag()"    , $txt );
  694.         $txt = preg_replace( "#\[quote=(.+?),\s*(.+?)\]#ie" , "\$this->regex_quote_tag('\\1', '\\2')"  , $txt );
  695.         $txt = preg_replace( "#\[/quote\]#ie"               , "\$this->regex_close_quote()"          , $txt );
  696.         
  697.         $txt = preg_replace( "/\n/", "<br>", $txt );
  698.         
  699.         if ( ($this->quote_open == $this->quote_closed) and ($this->quote_error == 0) ) {
  700.             // Preserve spacing
  701.             $txt = preg_replace( "#(<!--QuoteEBegin-->.+?<!--QuoteEnd-->)#es", "\$this->regex_preserve_spacing('\\1')", trim($txt) );
  702.             return $txt;
  703.         } else {
  704.             return $the_txt;
  705.         }
  706.         
  707.     }
  708.     
  709.     /**************************************************/
  710.     // regex_preserve_spacing: keeps double spaces
  711.     // without CSS killing <pre> tags
  712.     /**************************************************/
  713.     
  714.     function regex_preserve_spacing($txt="") {
  715.         $txt = preg_replace( "#\s{2}#", "  ", trim($txt) );
  716.         return $txt;
  717.     }
  718.     
  719.     /**************************************************/
  720.     // regex_simple_quote_tag: Builds this quote tag HTML
  721.     // [QUOTE] .. [/QUOTE]
  722.     /**************************************************/
  723.     
  724.     function regex_simple_quote_tag() {
  725.         global $ibforums;
  726.         
  727.         $this->quote_open++;
  728.  
  729.         return "<!--QuoteBegin-->{$this->quote_html['START']}<!--QuoteEBegin-->";
  730.         
  731.     }
  732.     
  733.     /**************************************************/
  734.     // regex_close_quote: closes a quote tag
  735.     // 
  736.     /**************************************************/
  737.     
  738.     function regex_close_quote() {
  739.     
  740.         if ($this->quote_open == 0) {
  741.             $this->quote_error++;
  742.              return;
  743.          }
  744.          
  745.          $this->quote_closed++;
  746.          
  747.          return "<!--QuoteEnd-->{$this->quote_html['END']}<!--QuoteEEnd-->";
  748.     }
  749.     
  750.     /**************************************************/
  751.     // regex_quote_tag: Builds this quote tag HTML
  752.     // [QUOTE=Matthew,14 February 2002]
  753.     /**************************************************/
  754.     
  755.     function regex_quote_tag($name="", $date="") {
  756.         global $ibforums;
  757.         
  758.         $default = "\[quote=$name,$date\]";
  759.         
  760.         $this->quote_open++;
  761.         
  762.         $html = $this->wrap_style( array( 'STYLE' => 'QUOTE', 'EXTRA' => "($name @ $date)" ) );
  763.         
  764.         $extra = "--$name+$date";
  765.         
  766.         return "<!--QuoteBegin".$extra."-->{$html['START']}<!--QuoteEBegin-->";
  767.         
  768.     }        
  769.     
  770.     /****************************************************************************************************/
  771.     // regex_check_flash: Checks, and builds the <object>
  772.     // html.
  773.     /**************************************************/
  774.     
  775.     function regex_check_flash($width="", $height="", $url="") {
  776.         global $ibforums;
  777.         
  778.         $default = "\[flash=$width,$height\]$url\[/flash\]";
  779.         
  780.         if (!$ibforums->vars['allow_flash']) {
  781.             return $default;
  782.         }
  783.         
  784.         if ($width > $ibforums->vars['max_w_flash']) {
  785.             $this->error = 'flash_too_big';
  786.             return $default;
  787.         }
  788.         
  789.         if ($height > $ibforums->vars['max_h_flash']) {
  790.             $this->error = 'flash_too_big';
  791.             return $default;
  792.         }
  793.         
  794.         if (!preg_match( "/^http:\/\/(\S+)\.swf$/i", $url) ) {
  795.             $this->error = 'flash_url';
  796.             return $default;
  797.         }
  798.         
  799.         return "<!--Flash $width+$height+$url--><OBJECT CLASSID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' WIDTH=$width HEIGHT=$height><PARAM NAME=MOVIE VALUE=$url><PARAM NAME=PLAY VALUE=TRUE><PARAM NAME=LOOP VALUE=TRUE><PARAM NAME=QUALITY VALUE=HIGH><EMBED SRC=$url WIDTH=$width HEIGHT=$height PLAY=TRUE LOOP=TRUE QUALITY=HIGH></EMBED></OBJECT><!--End Flash-->";
  800.     }
  801.     
  802.     /**************************************************/
  803.     // regex_check_image: Checks, and builds the <img>
  804.     // html.
  805.     /**************************************************/
  806.     
  807.     function regex_check_image($url="") {
  808.         global $ibforums;
  809.         
  810.         if (!$url) return;
  811.         
  812.         $url = trim($url);
  813.         
  814.         $default = "[img]".$url."[/img]";
  815.         
  816.         ++$this->image_count;
  817.         
  818.         // Make sure we've not overriden the set image # limit
  819.         
  820.         if ($ibforums->vars['max_images'])
  821.         {
  822.             if ($this->image_count > $ibforums->vars['max_images'])
  823.             {
  824.                 $this->error = 'too_many_img';
  825.                 return $default;
  826.             }
  827.         }
  828.         
  829.         // Are they attempting to post a dynamic image, or JS?
  830.         
  831.         if ($ibforums->vars['allow_dynamic_img'] != 1)
  832.         {
  833.             if (preg_match( "/[?&;]/", $url))
  834.             {
  835.                 $this->error = 'no_dynamic';
  836.                 return $default;
  837.             }
  838.             if (preg_match( "/javascript(\:|\s)/i", $url ))
  839.             {
  840.                 $this->error = 'no_dynamic';
  841.                 return $default;
  842.             }
  843.         }
  844.         
  845.         // Is the img extension allowed to be posted?
  846.         
  847.         if ($ibforums->vars['img_ext'])
  848.         {
  849.             $extension = preg_replace( "/^.*\.(\S+)$/", "\\1", $url );
  850.             $extension = strtolower($extension);
  851.             $ibforums->vars['img_ext'] = strtolower($ibforums->vars['img_ext']);
  852.             if (!preg_match( "/$extension(\||$)/", $ibforums->vars['img_ext'] ))
  853.             {
  854.                 $this->error = 'invalid_ext';
  855.                 return $default;
  856.             }
  857.         }
  858.         
  859.         // Is it a legitimate image?
  860.         
  861.         if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
  862.             $this->error = 'no_dynamic';
  863.             return $default;
  864.         }
  865.         
  866.         // If we are still here....
  867.         
  868.         return "<img src='$url' border='0' alt='user posted image'>";
  869.     }
  870.         
  871.     
  872.     /**************************************************/
  873.     // regex_font_attr:
  874.     // Returns a string for an /e regexp based on the input
  875.     /**************************************************/
  876.     
  877.     function regex_font_attr($IN) {
  878.         if (!is_array($IN)) return "";
  879.         
  880.         if ($IN['s'] == 'size')
  881.         {
  882.             $IN['1'] = $IN['1'] + 7;
  883.             
  884.             if ($IN['1'] > 30)
  885.             {
  886.                 $IN['1'] = 30;
  887.             }
  888.             
  889.             return "<span style='font-size:".$IN['1']."pt;line-height:100%'>".$IN['2']."</span>";
  890.         }
  891.         else if ($IN['s'] == 'col')
  892.         {
  893.             return "<span style='color:".$IN['1']."'>".$IN['2']."</span>";
  894.         }
  895.         else if ($IN['s'] == 'font')
  896.         {
  897.             return "<span style='font-family:".$IN['1']."'>".$IN['2']."</span>";
  898.         }
  899.     }
  900.     
  901.     /**************************************************/
  902.     // regex_build_url: Checks, and builds the a href
  903.     // html
  904.     /**************************************************/
  905.     
  906.     function regex_build_url($url=array()) {
  907.     
  908.         $skip_it = 0;
  909.         
  910.         // Make sure the last character isn't punctuation.. if it is, remove it and add it to the
  911.         // end array
  912.         
  913.         if ( preg_match( "/([\.,\?]|!)$/", $url['html'], $match) )
  914.         {
  915.             $url['end'] .= $match[1];
  916.             $url['html'] = preg_replace( "/([\.,\?]|!)$/", "", $url['html'] );
  917.             $url['show'] = preg_replace( "/([\.,\?]|!)$/", "", $url['show'] );
  918.         }
  919.         
  920.         // Make sure it's not being used in a closing code/quote/html or sql block
  921.         
  922.         if (preg_match( "/\[\/(html|quote|code|sql)/i", $url['html']) )
  923.         {
  924.             return $url['html'];
  925.         }
  926.         
  927.         // clean up the ampersands
  928.         $url['html'] = preg_replace( "/&/" , "&" , $url['html'] );
  929.         
  930.         // Make sure we don't have a JS link
  931.         $url['html'] = preg_replace( "/javascript:/i", "java script: ", $url['html'] );
  932.         
  933.         // Do we have http:// at the front?
  934.         
  935.         if ( ! preg_match("#^(http|news|https|ftp|aim)://#", $url['html'] ) )
  936.         {
  937.             $url['html'] = 'http://'.$url['html'];
  938.         }
  939.         
  940.         //-------------------------
  941.         // Tidy up the viewable URL
  942.         //-------------------------
  943.  
  944.         if (preg_match( "/^<img src/i", $url['show'] )) $skip_it = 1;
  945.  
  946.         $url['show'] = preg_replace( "/&/" , "&" , $url['show'] );
  947.         $url['show'] = preg_replace( "/javascript:/i", "javascript: ", $url['show'] );
  948.         
  949.         if (strlen($url['show']) < 55)  $skip_it = 1;
  950.         
  951.         // Make sure it's a "proper" url
  952.         
  953.         if (!preg_match( "/^(http|ftp|https|news):\/\//i", $url['show'] )) $skip_it = 1;
  954.         
  955.         $show     = $url['show'];
  956.         
  957.         if ($skip_it != 1) {
  958.             $stripped = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\2", $url['show'] );
  959.             $uri_type = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\1", $url['show'] );
  960.             
  961.             $show = $uri_type.'://'.substr( $stripped , 0, 35 ).'...'.substr( $stripped , -15   );
  962.         }
  963.         
  964.         return $url['st'] . "<a href='".$url['html']."' target='_blank'>".$show."</a>" . $url['end'];
  965.         
  966.     }
  967.     
  968. }
  969.  
  970.  
  971.  
  972. ?>